﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Winz.JsonModel;
using Winz.FoeBot;

/*
using Winz.DataSet;
using Winz.DataSet.foeDataSetTableAdapters;
*/


namespace Winz.FoeBot
{
    public class MotivatorPlayer
    {
        public OtherPlayer player;
        public int barRank;
        public BarsType bar;

        public MotivatorPlayer(OtherPlayer p, int rank, BarsType bar)
        {
            this.player = p;
            this.barRank = rank;
            this.bar = bar;
        }



        public Entity getBuidingToMotivate(OtherPlayeCity city, List<MotivateType> order, out MotivateType type)
        {

            Entity buildingToMotivate = null;

            List<MotivateType> tmpOrder = new List<MotivateType>(order);
            type = tmpOrder.First();

            while (tmpOrder.Count > 0 && buildingToMotivate == null)
            {
                type = tmpOrder.First();
                buildingToMotivate = getBuidingToMotivate(city, type);

                if (buildingToMotivate != null)
                {
                    return buildingToMotivate;
                }
                tmpOrder.RemoveAt(0);
            }
            return null;
        }




        public bool needMotivation()
        {
            return (!this.player.IsSelf
                && this.player.NextInteractionIn == 0
                && ((Bot.settings.visitFriends && bar == BarsType.Friends)
                     || (Bot.settings.visitGuild && bar == BarsType.Guild)
                     || (Bot.settings.visitNeighboors && bar == BarsType.Neighbour)
                     )
                );
        }
        


        public Entity getBuidingToMotivate(OtherPlayeCity city, MotivateType mType)
        {
            {
                
                Entity choosen = null;
                switch (mType)
                {
                    case MotivateType.PO:
                        choosen =
                            (from pe in city.CityMap.Entities.AsEnumerable()
                             join ce in Bot.cityEntities.AsEnumerable() on pe.CityentityId equals ce.Id
                             where ce.Type == "culture" && pe.State.Class != "PolishedState" && pe.State.Class != "ConstructionState" &&  pe.State.Class != "PlunderedState" && pe.Connected == 1
                             orderby ce.Points descending
                             select pe).FirstOrDefault();
                        break;
                    case MotivateType.MO:
                        choosen =
                        (from pe in city.CityMap.Entities.AsEnumerable()
                         join ce in Bot.cityEntities.AsEnumerable() on pe.CityentityId equals ce.Id
                         where ce.Type == "residential" 
                                && !pe.State.IsMotivated 
                                && pe.State.Class != "PolishedState" 
                                && pe.State.Class != "ConstructionState" 
                                && pe.State.Class != "PlunderedState" 
                                && pe.Connected == 1
                         orderby ce.Points descending
                         select pe).FirstOrDefault();
                        break;

                    case MotivateType.MM:
                        choosen =
                        (from pe in city.CityMap.Entities.AsEnumerable()
                         join ce in Bot.cityEntities.AsEnumerable() on pe.CityentityId equals ce.Id
                         where ce.Type == "production" && pe.State.Class != "IdleState" && pe.State.Class != "ProductionStaleState" && pe.State.Class != "PolishedState" && pe.State.Class != "ConstructionState" && pe.State.Class != "PlunderedState" && !pe.State.IsMotivated && pe.Connected == 1
                         orderby ce.Points descending
                         select pe).FirstOrDefault();
                        break;
                }
                return choosen;
                //            return new MapPoint(choosen.X,choosen.Y);
            }
        }
    }
}
